<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>使用PHP实现REST的HTTP客户端_北京达内科技VIP中心-北京C++培训、嵌入式培训、3G培训，最权威的培训机构</title>
<meta name="keywords" content="使用,PHP,实现,REST,的,HTTP,客户端,近来,出" />
<meta name="description" content="近来出现了很多REST的API，配合JSONP一起，很容易开发出结构清晰的应用。 我们最近在研究CouchDB。 它有一整套的REST API。 人们已经开发了很多php库，甚至 PHP 扩展来调用CouchDB。 我最喜欢" />
<link href="/templets/default/css/css.css"  rel="stylesheet" type="text/css" />
<link href="/templets/default/css/html.css" rel="stylesheet" type="text/css" />
<link href="/templets/default/css/style.css" type="text/css" rel="stylesheet" />
<link href="/templets/default/css/friendlink.css" type="text/css" rel="stylesheet" />
<link href="/templets/default/css/dnzx.css" rel="stylesheet" type="text/css" />
<script src="/templets/default/images/chinaz.js" type="text/javascript"></script>

</head>
<body>
<div class="top_kuan">
  <div class="top"><a href="/"><img src="/templets/default/n-img/logo.gif"title="达内LOGO" width="366" height="95" alt="达内LOGO和北京达内网址"/></a><img src="/templets/default/n-img/top_t.gif" width="422" height="95" title="达内IT培训之Java培训 3G培训 软件测试培训" alt="达内科技培训项目：Java培训 3G培训 Android培训 软件测试培训" /><img src="/templets/default/n-img/phone.gif" width="192" height="95" title="达内联系电话" alt="北京达内服务电话"/></div>
<div class="menu_kuan">
<div class="nav">
<div class="navinner">
  <ul class="navlist">
  <li><a href="/">达内首页</a></li>
  
      	<li><a href='/daneijianjie/' id='nav_1'  onmouseover="showMenu('nav_1','subnav1')">达内简介</a></li>
  
      	<li><a href='/danashizi/' id='nav_2'  onmouseover="showMenu('nav_2','subnav2')">达内师资</a></li>
  
      	<li><a href='/danakecheng/list_3_1.html' id='nav_3'  onmouseover="showMenu('nav_3','subnav3')">达内课程</a></li>
  
      	<li><a href='/danazixun/danaxinwen/list_10_1.html' id='nav_10'  onmouseover="showMenu('nav_10','subnav10')">达内资讯</a></li>
  
      	<li><a href='/zuixinjiuye/' id='nav_19'  onmouseover="showMenu('nav_19','subnav19')">就业资讯</a></li>
  
      	<li><a href='/gaoxinjiuye/' id='nav_20'  onmouseover="showMenu('nav_20','subnav20')">达内就业</a></li>
  
      	<li><a href='/zaixianbaoming/' id='nav_21'  onmouseover="showMenu('nav_21','subnav21')">在线报名</a></li>
  
      	<li><a href='http://www.dajiangsai.org/' id='nav_63428'  onmouseover="showMenu('nav_63428','subnav63428')">软件大赛</a></li>
  
   <li>
  	<div class="subnav disable" id="subnav3">
      <p class="pointer">.</p>
      <p><span>
  <a href="http://www.tarenansd.com/">Linux集群架构师</a>
  <a href="/danakecheng/waiqiC__jingpinjiuyeban/">外企C++精品就业班</a>
  <a href="/danakecheng/3Gqianrushi/">3G嵌入式</a>
  <a href="/danakecheng/ruanjianceshi/">软件测试</a>
  <a href="/danakecheng/javapeixun/">java培训</a>
  <a href="/danakecheng/PHPdianzishangwujingyingban/">PHP电子商务精英班</a>
  <a href="/danakecheng/_NET_yunjisuan/">.NET/云计算</a>
  <a href="/danakecheng/Android_3Ggaoduankecheng/">Android 3G高端课程</a>
  
  </span></p>
    </div>  
	</li><li>
  	<div class="subnav disable" id="subnav10">
      <p class="pointer">.</p>
      <p><span>
  <a href="/danazixun/danaxinwen/">达内新闻</a>
  <a href="/danazixun/jiuyekuaixun/">就业快讯</a>
  <a href="/danazixun/zhaoshengwenda/">招生问答</a>
  <a href="/danazixun/qiyezhaopin/">企业招聘</a>
  <a href="/danazixun/xueyuanganyan/">学员感言</a>
  <a href="/danazixun/zhiyesuzhike/">职业素质课</a>
  <a href="/danazixun/yuanxiaohezuo/">院校合作</a>
  
  </span></p>
    </div>  
	</li>
  </ul>
  <div class="menu_nav_right"></div>

</div>  
</div>
</div>
<!-- /header -->
<div class="biaot">
  <ul>
    <li class="mbx"><a href='http://www.vip-tarena.com/'>达内培训</a> > <a href='/PHPpeixun/'>PHP培训</a> > </li>
    <li class="times">24小时值班电话：<span style="color:#DF0000; font-size:18px;">13810478892</span></li>
  </ul>
</div>

<div class="content1">
	<div class="ndzx_left">
		
			<div id="apDiv31">
				<div id="apDiv38" class="bluel"><strong>PHP培训</strong></div>
			</div>
			 <div id="apDiv32">
				<div class="fwzc gray">
					<div class="ymtitle">使用PHP实现REST的HTTP客户端</div>
				    近来出现了很多REST的API，配合JSONP一起，很容易开发出结构清晰的应用。<br />
我们最近在研究CouchDB。 它有一整套的REST API。<br />
人们已经开发了很多php库，甚至<a href="http://www.vip-tarena.com">PHP</a>扩展来调用CouchDB。<br />
我最喜欢Zend Framework里面的REST client。<br />
但是为了练习一下，我决定自己写一个HTTP client。<br />
我的目的是：创建一个简单的class，可以向远程服务器发送GET ,POST, DELETE请求。<br />
<br />
虽然还没有开始进行设计与编码，但我想，我的class应该这样被调用：<br />
<br />
<pre class="prettyprint">
<span class="pln">echo </span><span class="typ">Http</span><span class="pun">::</span><span class="pln">connect</span><span class="pun">(</span><span class="str">&#39;localhost&#39;</span><span class="pun">,</span><span class="pln"> </span><span class="lit">8082</span><span class="pun">)</span><span class="pln">&nbsp;
&nbsp; &nbsp; </span><span class="pun">-&gt;</span><span class="pln">doGet</span><span class="pun">(</span><span class="str">&#39;tests/httpclient/dummy.php&#39;</span><span class="pun">,</span><span class="pln"> array</span><span class="pun">(</span><span class="str">&#39;a&#39;</span><span class="pln"> </span><span class="pun">=&gt;</span><span class="pln"> </span><span class="str">&quot;a a a&quot;</span><span class="pun">));</span></pre>
<br />
PS:这应该算是TDD吧，测试驱动开发。<br />
<br />
首先我要实现connect函数，在这个函数里面我不想真的去连接服务器。<br />
应该是在doGet的时候再去连接比较合适。<br />
就像lftp一样，当你在命令行输入<br />
<br />
<pre class="prettyprint">
<span class="pun">[</span><span class="pln">root@super tmp</span><span class="pun">]</span><span class="com"># lftp 192.168.0.100</span><span class="pln">&nbsp;
lftp </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">0.100</span><span class="pun">:~&gt;</span><span class="pln">user uuu &nbsp;ppp</span></pre>
<br />
这时其实没有进行真的连接。<br />
只有在你上传/下载/列表文件的时候，才会进行第一次连接。<br />
<br />
回到正题，我把connect作成了一个factory函数，它只是保存一下配置信息，<br />
然后返回一个本类的实例。<br />
<br />
<pre class="prettyprint">
<span class="kwd">private</span><span class="pln"> $_host </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">null</span><span class="pun">;</span><span class="pln">&nbsp;
</span><span class="kwd">private</span><span class="pln"> $_port </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">null</span><span class="pun">;</span><span class="pln">&nbsp;
</span><span class="kwd">private</span><span class="pln"> $_user </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">null</span><span class="pun">;</span><span class="pln">&nbsp;
</span><span class="kwd">private</span><span class="pln"> $_pass </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">null</span><span class="pun">;</span><span class="pln">&nbsp;
&nbsp;&nbsp;
</span><span class="com">/**&nbsp;
&nbsp;* Factory of the class. Lazy connect&nbsp;
&nbsp;*&nbsp;
&nbsp;* @param string $host&nbsp;
&nbsp;* @param integer $port&nbsp;
&nbsp;* @param string $user&nbsp;
&nbsp;* @param string $pass&nbsp;
&nbsp;* @return Http&nbsp;
&nbsp;*/</span><span class="pln">&nbsp;
</span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">function</span><span class="pln"> connect</span><span class="pun">(</span><span class="pln">$host</span><span class="pun">,</span><span class="pln"> $port</span><span class="pun">,</span><span class="pln"> $user</span><span class="pun">=</span><span class="kwd">null</span><span class="pun">,</span><span class="pln"> $pass</span><span class="pun">=</span><span class="kwd">null</span><span class="pun">)</span><span class="pln">&nbsp;
</span><span class="pun">{</span><span class="pln">&nbsp;
&nbsp; &nbsp; </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">(</span><span class="pln">$host</span><span class="pun">,</span><span class="pln"> $port</span><span class="pun">,</span><span class="pln"> $user</span><span class="pun">,</span><span class="pln"> $pass</span><span class="pun">);</span><span class="pln">&nbsp;
</span><span class="pun">}</span><span class="pln">&nbsp;
</span><span class="kwd">protected</span><span class="pln"> </span><span class="kwd">function</span><span class="pln"> __construct</span><span class="pun">(</span><span class="pln">$host</span><span class="pun">,</span><span class="pln"> $port</span><span class="pun">,</span><span class="pln"> $user</span><span class="pun">,</span><span class="pln"> $pass</span><span class="pun">)</span><span class="pln">&nbsp;
</span><span class="pun">{</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; $this</span><span class="pun">-&gt;</span><span class="pln">_host </span><span class="pun">=</span><span class="pln"> $host</span><span class="pun">;</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; $this</span><span class="pun">-&gt;</span><span class="pln">_port </span><span class="pun">=</span><span class="pln"> $port</span><span class="pun">;</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; $this</span><span class="pun">-&gt;</span><span class="pln">_user </span><span class="pun">=</span><span class="pln"> $user</span><span class="pun">;</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; $this</span><span class="pun">-&gt;</span><span class="pln">_pass </span><span class="pun">=</span><span class="pln"> $pass</span><span class="pun">;</span><span class="pln">&nbsp;
</span><span class="pun">}</span></pre>
<br />
下面为每种操作方式写函数(doGet,doPost,doDelete)<br />
<br />
<pre class="prettyprint">
<span class="kwd">const</span><span class="pln"> POST &nbsp; </span><span class="pun">=</span><span class="pln"> </span><span class="str">&#39;POST&#39;</span><span class="pun">;</span><span class="pln">&nbsp;
</span><span class="kwd">const</span><span class="pln"> GET &nbsp; &nbsp;</span><span class="pun">=</span><span class="pln"> </span><span class="str">&#39;GET&#39;</span><span class="pun">;</span><span class="pln">&nbsp;
</span><span class="kwd">const</span><span class="pln"> DELETE </span><span class="pun">=</span><span class="pln"> </span><span class="str">&#39;DELETE&#39;</span><span class="pun">;</span><span class="pln">&nbsp;
&nbsp;&nbsp;
</span><span class="com">/**&nbsp;
&nbsp;* POST request&nbsp;
&nbsp;*&nbsp;
&nbsp;* @param string $url&nbsp;
&nbsp;* @param array $params&nbsp;
&nbsp;* @return string&nbsp;
&nbsp;*/</span><span class="pln">&nbsp;
</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">function</span><span class="pln"> doPost</span><span class="pun">(</span><span class="pln">$url</span><span class="pun">,</span><span class="pln"> $params</span><span class="pun">=</span><span class="pln">array</span><span class="pun">())</span><span class="pln">&nbsp;
</span><span class="pun">{</span><span class="pln">&nbsp;
&nbsp; &nbsp; </span><span class="kwd">return</span><span class="pln"> $this</span><span class="pun">-&gt;</span><span class="pln">_exec</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">::</span><span class="pln">POST</span><span class="pun">,</span><span class="pln"> $this</span><span class="pun">-&gt;</span><span class="pln">_url</span><span class="pun">(</span><span class="pln">$url</span><span class="pun">),</span><span class="pln"> $params</span><span class="pun">);</span><span class="pln">&nbsp;
</span><span class="pun">}</span><span class="pln">&nbsp;
&nbsp;&nbsp;
</span><span class="com">/**&nbsp;
&nbsp;* GET Request&nbsp;
&nbsp;*&nbsp;
&nbsp;* @param string $url&nbsp;
&nbsp;* @param array $params&nbsp;
&nbsp;* @return string&nbsp;
&nbsp;*/</span><span class="pln">&nbsp;
</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">function</span><span class="pln"> doGet</span><span class="pun">(</span><span class="pln">$url</span><span class="pun">,</span><span class="pln"> $params</span><span class="pun">=</span><span class="pln">array</span><span class="pun">())</span><span class="pln">&nbsp;
</span><span class="pun">{</span><span class="pln">&nbsp;
&nbsp; &nbsp; </span><span class="kwd">return</span><span class="pln"> $this</span><span class="pun">-&gt;</span><span class="pln">_exec</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">::</span><span class="pln">GET</span><span class="pun">,</span><span class="pln"> $this</span><span class="pun">-&gt;</span><span class="pln">_url</span><span class="pun">(</span><span class="pln">$url</span><span class="pun">),</span><span class="pln"> $params</span><span class="pun">);</span><span class="pln">&nbsp;
</span><span class="pun">}</span><span class="pln">&nbsp;
&nbsp;&nbsp;
</span><span class="com">/**&nbsp;
&nbsp;* DELETE Request&nbsp;
&nbsp;*&nbsp;
&nbsp;* @param string $url&nbsp;
&nbsp;* @param array $params&nbsp;
&nbsp;* @return string&nbsp;
&nbsp;*/</span><span class="pln">&nbsp;
</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">function</span><span class="pln"> doDelete</span><span class="pun">(</span><span class="pln">$url</span><span class="pun">,</span><span class="pln"> $params</span><span class="pun">=</span><span class="pln">array</span><span class="pun">())</span><span class="pln">&nbsp;
</span><span class="pun">{</span><span class="pln">&nbsp;
&nbsp; &nbsp; </span><span class="kwd">return</span><span class="pln"> $this</span><span class="pun">-&gt;</span><span class="pln">_exec</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">::</span><span class="pln">DELETE</span><span class="pun">,</span><span class="pln"> $this</span><span class="pun">-&gt;</span><span class="pln">_url</span><span class="pun">(</span><span class="pln">$url</span><span class="pun">),</span><span class="pln"> $params</span><span class="pun">);</span><span class="pln">&nbsp;
</span><span class="pun">}</span></pre>
<br />
它们只是去调用了 _exec()函数。<br />
<br />
再加入一个设置Header的函数：<br />
<br />
<pre class="prettyprint">
<span class="kwd">private</span><span class="pln"> $_headers </span><span class="pun">=</span><span class="pln"> array</span><span class="pun">();</span><span class="pln">&nbsp;
</span><span class="com">/**&nbsp;
&nbsp;* setHeaders&nbsp;
&nbsp;*&nbsp;
&nbsp;* @param array $headers&nbsp;
&nbsp;* @return Http&nbsp;
&nbsp;*/</span><span class="pln">&nbsp;
</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">function</span><span class="pln"> setHeaders</span><span class="pun">(</span><span class="pln">$headers</span><span class="pun">)</span><span class="pln">&nbsp;
</span><span class="pun">{</span><span class="pln">&nbsp;
&nbsp; &nbsp; $this</span><span class="pun">-&gt;</span><span class="pln">_headers </span><span class="pun">=</span><span class="pln"> $headers</span><span class="pun">;</span><span class="pln">&nbsp;
&nbsp; &nbsp; </span><span class="kwd">return</span><span class="pln"> $this</span><span class="pun">;</span><span class="pln">&nbsp;
</span><span class="pun">}</span></pre>
<br />
每个函数都返回了这个类的实例，是因为我比较喜欢链式写法。<br />
就像下面这样：<br />
<br />
<pre class="prettyprint">
<span class="pln">echo </span><span class="typ">Http</span><span class="pun">::</span><span class="pln">connect</span><span class="pun">(</span><span class="str">&#39;localhost&#39;</span><span class="pun">,</span><span class="pln"> </span><span class="lit">8082</span><span class="pun">)</span><span class="pln">&nbsp;
&nbsp; &nbsp; </span><span class="pun">-&gt;</span><span class="pln">setHeaders</span><span class="pun">(</span><span class="pln">$myCustomHeaders</span><span class="pun">)</span><span class="pln">&nbsp;
&nbsp; &nbsp; </span><span class="pun">-&gt;</span><span class="pln">doGet</span><span class="pun">(</span><span class="str">&#39;tests/httpclient/dummy.php&#39;</span><span class="pun">,</span><span class="pln"> array</span><span class="pun">(</span><span class="str">&#39;a&#39;</span><span class="pln"> </span><span class="pun">=&gt;</span><span class="pln"> </span><span class="str">&quot;a a a&quot;</span><span class="pun">));</span></pre>
<br />
最后来看 _exec()函数。在PHP里面，有很多方法来实现它的功能，<br />
在这里我用了CURL。 请看一下phpinfo()，确认你的<a href="http://www.vip-tarena.com">php</a>支持不支持curl。<br />
不支持的时候，您可以使用socket函数。<br />
<br />
<pre class="prettyprint">
<span class="kwd">const</span><span class="pln"> HTTP_OK </span><span class="pun">=</span><span class="pln"> </span><span class="lit">200</span><span class="pun">;</span><span class="pln">&nbsp;
</span><span class="kwd">const</span><span class="pln"> HTTP_CREATED </span><span class="pun">=</span><span class="pln"> </span><span class="lit">201</span><span class="pun">;</span><span class="pln">&nbsp;
</span><span class="kwd">const</span><span class="pln"> HTTP_ACEPTED </span><span class="pun">=</span><span class="pln"> </span><span class="lit">202</span><span class="pun">;</span><span class="pln">&nbsp;
&nbsp;&nbsp;
</span><span class="com">/**&nbsp;
&nbsp;* Performing the real request&nbsp;
&nbsp;*&nbsp;
&nbsp;* @param string $type&nbsp;
&nbsp;* @param string $url&nbsp;
&nbsp;* @param array $params&nbsp;
&nbsp;* @return string&nbsp;
&nbsp;*/</span><span class="pln">&nbsp;
</span><span class="kwd">private</span><span class="pln"> </span><span class="kwd">function</span><span class="pln"> _exec</span><span class="pun">(</span><span class="pln">$type</span><span class="pun">,</span><span class="pln"> $url</span><span class="pun">,</span><span class="pln"> $params </span><span class="pun">=</span><span class="pln"> array</span><span class="pun">())</span><span class="pln">&nbsp;
</span><span class="pun">{</span><span class="pln">&nbsp;
&nbsp; &nbsp; $headers </span><span class="pun">=</span><span class="pln"> $this</span><span class="pun">-&gt;</span><span class="pln">_headers</span><span class="pun">;</span><span class="pln">&nbsp;
&nbsp; &nbsp; $s </span><span class="pun">=</span><span class="pln"> curl_init</span><span class="pun">();</span><span class="pln">&nbsp;
&nbsp;&nbsp;
&nbsp; &nbsp; </span><span class="kwd">if</span><span class="pun">(!</span><span class="pln">is_null</span><span class="pun">(</span><span class="pln">$this</span><span class="pun">-&gt;</span><span class="pln">_user</span><span class="pun">)){</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;curl_setopt</span><span class="pun">(</span><span class="pln">$s</span><span class="pun">,</span><span class="pln"> CURLOPT_USERPWD</span><span class="pun">,</span><span class="pln"> $this</span><span class="pun">-&gt;</span><span class="pln">_user</span><span class="pun">.</span><span class="str">&#39;:&#39;</span><span class="pun">.</span><span class="pln">$this</span><span class="pun">-&gt;</span><span class="pln">_pass</span><span class="pun">);</span><span class="pln">&nbsp;
&nbsp; &nbsp; </span><span class="pun">}</span><span class="pln">&nbsp;
&nbsp;&nbsp;
&nbsp; &nbsp; </span><span class="kwd">switch</span><span class="pln"> </span><span class="pun">(</span><span class="pln">$type</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="kwd">case</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">::</span><span class="pln">DELETE</span><span class="pun">:</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curl_setopt</span><span class="pun">(</span><span class="pln">$s</span><span class="pun">,</span><span class="pln"> CURLOPT_URL</span><span class="pun">,</span><span class="pln"> $url </span><span class="pun">.</span><span class="pln"> </span><span class="str">&#39;?&#39;</span><span class="pln"> </span><span class="pun">.</span><span class="pln"> http_build_query</span><span class="pun">(</span><span class="pln">$params</span><span class="pun">));</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curl_setopt</span><span class="pun">(</span><span class="pln">$s</span><span class="pun">,</span><span class="pln"> CURLOPT_CUSTOMREQUEST</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">::</span><span class="pln">DELETE</span><span class="pun">);</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="kwd">break</span><span class="pun">;</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="kwd">case</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">::</span><span class="pln">POST</span><span class="pun">:</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curl_setopt</span><span class="pun">(</span><span class="pln">$s</span><span class="pun">,</span><span class="pln"> CURLOPT_URL</span><span class="pun">,</span><span class="pln"> $url</span><span class="pun">);</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curl_setopt</span><span class="pun">(</span><span class="pln">$s</span><span class="pun">,</span><span class="pln"> CURLOPT_POST</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">);</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curl_setopt</span><span class="pun">(</span><span class="pln">$s</span><span class="pun">,</span><span class="pln"> CURLOPT_POSTFIELDS</span><span class="pun">,</span><span class="pln"> $params</span><span class="pun">);</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="kwd">break</span><span class="pun">;</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="kwd">case</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">::</span><span class="pln">GET</span><span class="pun">:</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curl_setopt</span><span class="pun">(</span><span class="pln">$s</span><span class="pun">,</span><span class="pln"> CURLOPT_URL</span><span class="pun">,</span><span class="pln"> $url </span><span class="pun">.</span><span class="pln"> </span><span class="str">&#39;?&#39;</span><span class="pln"> </span><span class="pun">.</span><span class="pln"> http_build_query</span><span class="pun">(</span><span class="pln">$params</span><span class="pun">));</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="kwd">break</span><span class="pun">;</span><span class="pln">&nbsp;
&nbsp; &nbsp; </span><span class="pun">}</span><span class="pln">&nbsp;
&nbsp;&nbsp;
&nbsp; &nbsp; curl_setopt</span><span class="pun">(</span><span class="pln">$s</span><span class="pun">,</span><span class="pln"> CURLOPT_RETURNTRANSFER</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">);</span><span class="pln">&nbsp;
&nbsp; &nbsp; curl_setopt</span><span class="pun">(</span><span class="pln">$s</span><span class="pun">,</span><span class="pln"> CURLOPT_HTTPHEADER</span><span class="pun">,</span><span class="pln"> $headers</span><span class="pun">);</span><span class="pln">&nbsp;
&nbsp; &nbsp; $_out </span><span class="pun">=</span><span class="pln"> curl_exec</span><span class="pun">(</span><span class="pln">$s</span><span class="pun">);</span><span class="pln">&nbsp;
&nbsp; &nbsp; $status </span><span class="pun">=</span><span class="pln"> curl_getinfo</span><span class="pun">(</span><span class="pln">$s</span><span class="pun">,</span><span class="pln"> CURLINFO_HTTP_CODE</span><span class="pun">);</span><span class="pln">&nbsp;
&nbsp; &nbsp; curl_close</span><span class="pun">(</span><span class="pln">$s</span><span class="pun">);</span><span class="pln">&nbsp;
&nbsp; &nbsp; </span><span class="kwd">switch</span><span class="pln"> </span><span class="pun">(</span><span class="pln">$status</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="kwd">case</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">::</span><span class="pln">HTTP_OK</span><span class="pun">:</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="kwd">case</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">::</span><span class="pln">HTTP_CREATED</span><span class="pun">:</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="kwd">case</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">::</span><span class="pln">HTTP_ACEPTED</span><span class="pun">:</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $out </span><span class="pun">=</span><span class="pln"> $_out</span><span class="pun">;</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="kwd">break</span><span class="pun">;</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="kwd">default</span><span class="pun">:</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="kwd">throw</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">Http_Exception</span><span class="pun">(</span><span class="str">&quot;http error: {$status}&quot;</span><span class="pun">,</span><span class="pln"> $status</span><span class="pun">);</span><span class="pln">&nbsp;
&nbsp; &nbsp; </span><span class="pun">}</span><span class="pln">&nbsp;
&nbsp; &nbsp; </span><span class="kwd">return</span><span class="pln"> $out</span><span class="pun">;</span><span class="pln">&nbsp;
</span><span class="pun">}</span></pre>
<br />
恩，这里使用了一个 Http_Exception 的异常处理类。<br />
<br />
<pre class="prettyprint">
<span class="kwd">class</span><span class="pln"> </span><span class="typ">Http_Exception</span><span class="pln"> </span><span class="kwd">extends</span><span class="pln"> </span><span class="typ">Exception</span><span class="pun">{</span><span class="pln">&nbsp;
&nbsp; &nbsp; </span><span class="kwd">const</span><span class="pln"> NOT_MODIFIED </span><span class="pun">=</span><span class="pln"> </span><span class="lit">304</span><span class="pun">;</span><span class="pln">&nbsp;
&nbsp; &nbsp; </span><span class="kwd">const</span><span class="pln"> BAD_REQUEST </span><span class="pun">=</span><span class="pln"> </span><span class="lit">400</span><span class="pun">;</span><span class="pln">&nbsp;
&nbsp; &nbsp; </span><span class="kwd">const</span><span class="pln"> NOT_FOUND </span><span class="pun">=</span><span class="pln"> </span><span class="lit">404</span><span class="pun">;</span><span class="pln">&nbsp;
&nbsp; &nbsp; </span><span class="kwd">const</span><span class="pln"> NOT_ALOWED </span><span class="pun">=</span><span class="pln"> </span><span class="lit">405</span><span class="pun">;</span><span class="pln">&nbsp;
&nbsp; &nbsp; </span><span class="kwd">const</span><span class="pln"> CONFLICT </span><span class="pun">=</span><span class="pln"> </span><span class="lit">409</span><span class="pun">;</span><span class="pln">&nbsp;
&nbsp; &nbsp; </span><span class="kwd">const</span><span class="pln"> PRECONDITION_FAILED </span><span class="pun">=</span><span class="pln"> </span><span class="lit">412</span><span class="pun">;</span><span class="pln">&nbsp;
&nbsp; &nbsp; </span><span class="kwd">const</span><span class="pln"> INTERNAL_ERROR </span><span class="pun">=</span><span class="pln"> </span><span class="lit">500</span><span class="pun">;</span><span class="pln">&nbsp;
</span><span class="pun">}</span></pre>
<br />
在向远程CouchDB服务器请求的过程中，如果出错，CouchDB会返回标准的HTTP STATUS CODE。所以上面的状态码，您应该是蛮熟悉的才对。<br />
<br />
异常捕捉代码：<br />
<br />
<pre class="prettyprint">
<span class="kwd">try</span><span class="pln"> </span><span class="pun">{</span><span class="pln">&nbsp;
&nbsp; &nbsp;echo </span><span class="typ">Http</span><span class="pun">::</span><span class="pln">connect</span><span class="pun">(</span><span class="str">&#39;localhost&#39;</span><span class="pun">,</span><span class="pln"> </span><span class="lit">8082</span><span class="pun">)</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="pun">-&gt;</span><span class="pln">doGet</span><span class="pun">(</span><span class="str">&#39;tests/couchdb/a.php&#39;</span><span class="pun">,</span><span class="pln"> array</span><span class="pun">(</span><span class="str">&#39;a&#39;</span><span class="pln"> </span><span class="pun">=&gt;</span><span class="pln"> </span><span class="str">&quot;a a a&quot;</span><span class="pun">));</span><span class="pln">&nbsp;
</span><span class="pun">}</span><span class="pln"> </span><span class="kwd">catch</span><span class="pln"> </span><span class="pun">(</span><span class="typ">Http_Exception</span><span class="pln"> $e</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">&nbsp;
&nbsp; &nbsp; </span><span class="kwd">switch</span><span class="pln"> </span><span class="pun">(</span><span class="pln">$e</span><span class="pun">-&gt;</span><span class="pln">getCode</span><span class="pun">())</span><span class="pln"> </span><span class="pun">{</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="kwd">case</span><span class="pln"> </span><span class="typ">Http_Exception</span><span class="pun">::</span><span class="pln">INTERNAL_ERROR</span><span class="pun">:</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="com">// do something</span><span class="pln">&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="kwd">break</span><span class="pun">;</span><span class="pln">&nbsp;
&nbsp; &nbsp; </span><span class="pun">}</span><span class="pln">&nbsp;
</span><span class="pun">}</span></pre>
<br />

				</div> 
           </div>
           <div id="zpDiv32"></div>
           <div id="apDiv35"><img src="/templets/default/images/cpzx-05.jpg" width="735" height="23" /></div>
  </div>
	<div class="caidan">
			<div id="apDiv30"><img src="/templets/default/images/xwzx-03.jpg" width="247" height="57" /></div>
			<div id="apDiv33">
				<div class="zdh">
				
					<div id='xpDiv36'>
			   <div id='apDiv37' class='linkzdh'><a href="/qianrushipeixun/">嵌入式培训</a></div>
			</div><div id='xpDiv36'>
			   <div id='apDiv37' class='linkzdh'><a href="/C__peixun/">C++培训</a></div>
			</div><div id='xpDiv36'>
			   <div id='apDiv37' class='linkzdh'><a href="/PHPpeixun/">PHP培训</a></div>
			</div><div id='xpDiv36'>
			   <div id='apDiv37' class='linkzdh'><a href="/javapeixun/">java培训</a></div>
			</div>
				
				</div>
			</div>
			<div id="apDiv34"><img src="/templets/default/images/cpzx-03.jpg" width="247" height="23" /></div>
	</div>
    
</div>

<link href="/templets/default/style/fudong1.css" rel="stylesheet" media="screen" type="text/css" />
<div id="floatLeftDiv1"  onclick="window.open('http://www.3gtarena.com/baidutgzt/wlgc/index.html');">
</div>


<div id="ft">
  <div class="bottom_bg">
    <div id="bottom">
      <!--S-logo-->
      <div class="logo"><a href="http://www.vip-tarena.com"><img src="/templets/default/images/logo_b.jpg" alt="北京达内" title="北京达内"/></a></div>
      <!--E-logo-->
      <!--S-关于我们-->
      <div class="links">
        <ul class="about">
		<li><a href="/daneijianjie/" target="_blank">关于达内</a></li>
          <li>|</li>
          <li><a href="http://www.bjtarena.com/shiting.html" target="_blank">在线试听</a></li>
          <li>|</li>
          <li><a href="http://chat.looyu.com/chat/chat/p.do?c=51083&f=114021&g=73339" target="_blank" rel="nofollow">联系我们</a></li>
          <li>|</li>
          <li><a href="http://wpa.qq.com/msgrd?v=3&uin=33640561&site=qq&menu=yes">企业合作</a></li>
          <li>|</li>
          <li><a href="/sitemap.xml" target="_blank">网站地图</a></li>
        </ul>
        <div class="blank_1"></div>
        <!--S-更多信息-->
        <div class="more_info">
          <ul>
            <li>达内先就业后付款，高端C++培训,嵌入式培训,Java培训,3G-Android培训,软件测试培训，助您就业国内外IT名企！</li>
            <li>达内地址：北京市东城区珠市口东大街6号（中国新闻大厦对面）　  24小时咨询热线:400-6040-456 公司座机： 010-67028668    </li>
            <li>2002-2011 　达内科技(中国)有限公司　 版权所有 　京ICP备08000853号-32　　<script type="text/javascript">
var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://");
document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3Ff56cc95c8c01ad9baf62ba43d510d81b' type='text/javascript'%3E%3C/script%3E"));
</script>　<script language="javascript" type="text/javascript" src="http://js.users.51.la/5391240.js"></script>
<noscript><a href="http://www.51.la/?5391240" target="_blank" rel="nofollow"><img alt="&#x6211;&#x8981;&#x5566;&#x514D;&#x8D39;&#x7EDF;&#x8BA1;" src="http://img.users.51.la/5391240.asp" style="border:none" /></a></noscript>      

<a href="http://webscan.360.cn/index/checkwebsite/url/www.vip-tarena.com" rel="nofollow"><img border="0" width="64px" height="24px"  src="http://img.webscan.360.cn/status/pai/hash/6fae5bdbcc08fbc117ded7f5436ae3b6"/></a>
</li>
          </ul>
        </div>
        <!--E-更多信息-->
      </div>
    </div>
  </div>
</div>

<script language="javascript" src="http://gate.looyu.com/51083/114021.js"></script>
<!-- /footer -->
</body>
</html>
